package com.woniuxy.house.admin;

import com.woniuxy.house.entity.PageBean;
import com.woniuxy.house.entity.Post;
import com.woniuxy.house.service.PostService;
import com.woniuxy.house.utils.JwtUtil;
import com.woniuxy.house.vo.PostVo;
import com.woniuxy.house.vo.ResponseEntity;
import com.woniuxy.house.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台帖子
 */
@RestController("admincontroller")
@RequestMapping("/post")
public class PostController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private PostService postService;

    @RequestMapping("/list")//初始化查询
    public ResponseEntity list(@RequestHeader String jwt){
        if (jwtUtil.verifyJWT(jwt)) {
            PageBean<Post> page = postService.getByPage(1);//这里是一开始初始化 默认查第一页数据
            return  new ResponseEntity("200","成功",page);
        }else{
            return  new ResponseEntity("700","令牌失效","null");
        }
    }

    @RequestMapping("/getpage/{page}")//后续的分页查询
    public ResponseEntity getPage(@PathVariable int page){
        PageBean<Post> byPage = postService.getByPage(page);
        return   new ResponseEntity("200","成功",byPage);
    }


    @RequestMapping("/reset")
    public ResponseEntity reset(){
        PageBean<Post> byPage = postService.getByPage(1);
        return  new ResponseEntity("200","成功",byPage);
    }

    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody Post post){
        postService.updateByCondition(post);

        return  ResponseEntity.SUCCESS;
    }

    @RequestMapping("/delete")
    public ResponseEntity delete(@RequestBody Post post){
        //post.setIsDelete("Y");
        postService.updateByCondition(post);
        return  ResponseEntity.SUCCESS;
    }
    //多条件查询
    @RequestMapping("/condition")
    public ResponseEntity getByCondition(@RequestBody SearchVo searchVo){
        PageBean<Post> pageBySearchText = postService.getPageBySearchText(searchVo.getText(), searchVo.getPage());

        //PageBean<Post> pageBySearchText = postService.getPageByCondition(postvo.getPost(),postvo.getPage());
        return  new ResponseEntity("200","成功",pageBySearchText);
    }

}
